<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>钱包盒子</title>
		<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<link rel="stylesheet" href="css/mui.css" />
		<link rel="stylesheet" href="css/public.css" />
	</head>

	<body>
		<header id="title" class="mui-bar mui-bar-nav">
			<h1 class="mui-title">首页</h1>
		</header>
		
		<nav class="mui-bar mui-bar-tab">
			<a diytab="0" class="mui-tab-item mui-active" href="html/main.html" name="main">
				<span class="iconfont icon-index"></span>
				<span class="mui-tab-label">首页</span>
			</a>
			<a diytab="1" class="mui-tab-item" href="html/find.html" name="find">
				<span class="iconfont icon-find"></span>
				<span class="mui-tab-label">发现</span>
			</a>
			<a diytab="2" class="mui-tab-item" href="html/my.html" name="my">
				<span class="iconfont icon-my"></span>
				<span class="mui-tab-label">我的</span>
			</a>
		</nav>
	</body>
	<script src="js/mui.js"></script>
	<script src="js/common.js" type="text/javascript" charset="utf-8"></script>
	<script type="text/javascript" charset="utf-8">
		//mui初始化
		mui.init();
		var subpages = [{
			path: 'html/main.html',
			name: 'main'
		}, {
			path: 'html/find.html',
			name: 'find'
		}, {
			path: 'html/service.html',
			name: 'service'
		}, {
			path: 'html/my.html',
			name: 'my'
		}];
		var subpage_style = {
			top: '44px',
			bottom: '51px'
		};

		var aniShow = {};

		//创建子页面，首个选项卡页面显示，其它均隐藏；
		mui.plusReady(function() {
			var self = plus.webview.currentWebview();
			for(var i = 0; i < 4; i++) {
				var temp = {};
				var sub = plus.webview.create(subpages[i]['path'], subpages[i]['name'], subpage_style);
				if(i > 0) {
					sub.hide();
				} else {
					temp[subpages[i]['name']] = "true";
					mui.extend(aniShow, temp);
				}
				self.append(sub);
			}
		});
		
		//当前激活选项
		var activeTab = subpages[0]['name'];
		var title = document.getElementById("title");
		
		//选项卡点击事件
		mui('.mui-bar-tab').on('tap', 'a', function(e) {
			var targetTab = this.getAttribute('name');
			if(targetTab == activeTab) {
				return;
			}
			//更换标题
			switch (this.getAttribute("diyTab")) {
				case '0':
					title.innerHTML = '<h1 class="mui-title">首页</h1>';
					break;
				case '1':
					title.innerHTML = '<div class="w60 mlr-auto">' +
						'<div id="SegmentedControl" class="mui-segmented-control">' +
						'<a class="mui-control-item mui-active" id="left">服务</a>' +
						'<a class="mui-control-item" id="right">社区</a>' +
						'</div>' +
						'</div>';
						addressToggle();
					
					break;
				case '2':
					title.innerHTML = '<h1 class="mui-title">我的</h1>';
					break;
			}
			//显示目标选项卡
			//若为iOS平台或非首次显示，则直接显示
			if(mui.os.ios || aniShow[targetTab]) {
				plus.webview.show(targetTab);
			} else {
				//否则，使用fade-in动画，且保存变量
				var temp = {};
				temp[targetTab] = "true";
				mui.extend(aniShow, temp);
				plus.webview.show(targetTab, "fade-in", 300);
			}
			//隐藏当前;
			plus.webview.hide(activeTab);
			//更改当前活跃的选项卡
			activeTab = targetTab;
		});
		
		function addressToggle() {
			var right = document.getElementById('SegmentedControl');
			right.addEventListener('tap', function(e) {
				if (e.target.id == 'right') {
					e.target.setAttribute('class', 'mui-control-item mui-active');
				    e.target.previousSibling.setAttribute('class', 'mui-control-item');	
					plus.webview.getWebviewById('main').hide();
					plus.webview.getWebviewById('find').hide();
					plus.webview.getWebviewById('my').hide();
					plus.webview.getWebviewById('service').show();
				}
				if (e.target.id == 'left') {
					e.target.setAttribute('class', 'mui-control-item mui-active');
				    e.target.nextSibling.setAttribute('class', 'mui-control-item');	
					plus.webview.getWebviewById('main').hide();
					plus.webview.getWebviewById('my').hide();
					plus.webview.getWebviewById('service').hide();
					plus.webview.getWebviewById('find').show();
				}					
			})
		}
	</script>

</html>